<template>
    <div class='registration-wrapper'>
        <div class='login-tel'>
            <input type="text" v-model="userTel" placeholder="请输入手机号" pattern="[0-9]*">
        </div>
        <div class='login-code'>
            <input type="text" v-model="userCode" placeholder="请输入短信验证码" pattern="[0-9]*" >
            <button :disabled="disabled" @click='sendCode'>{{ codeMsg }}</button>
        </div>
        <div class='login-tel'>
            <input type="text" v-model="userPwd" placeholder="请设置密码" pattern="[0-9]*">
        </div>
        <div class="login-btn">
            <button @click="registration">注 册</button>
        </div>
        <div class='tab'>
            <span @click='goPwdLogin'>密码登录</span>
            <span @click='goSmsLogin'>短信登录</span>
    	</div>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
import { AddUserModel } from 'models/account';
import { CodeModel } from 'models/code'
import tools from 'utils/tools.js';

export default {
    name:'RegistrationWrapper',
    data() {
        return {
            disabled: false,
            userTel: '',
            userPwd: '',
            userCode: '',
            codeNum: 6,
            codeMsg: '获取短信验证码',
            code: ''
        }
    },
    methods: {
        //点击获取短信验证码按钮
        sendCode() {
            //前端验证
            if (!tools.validate('userTel', this.userTel)) return;

            //请求短信验证码接口
            const codeModel = new CodeModel();
            codeModel.code(this.userTel).then(res => {
                const data = res.data;

                console.log(data.msg + ':' + data.data);
                if (data.success) {
                    this.code = data.data;
                }
            })
            //禁用按钮
            this.disabled = true;
            //倒计时
            let timer = setInterval(() => {
                --this.codeNum;
                this.codeMsg = `重新发送 ${this.codeNum}`;
            }, 1000)
            //判断什么时候停止定时器
            setTimeout(() => {
                clearInterval(timer);
                this.codeNum = 6;
                this.disabled = false;
                this.codeMsg = '获取短信验证码';
            }, 6000)
        },
        // 密码登录
        goPwdLogin() {
            this.$router.push('/pwdLogin');
        },
        // 短信登录
        goSmsLogin() {
            this.$router.push('/');
        },
        //点击注册
        registration() {
             //前端验证
            if (!tools.validate('userTel', this.userTel)) return;
            if (!tools.validate('userCode', this.userCode)) return;
            if (!tools.validate('userPwd', this.userPwd)) return;

            //判断验证码是否正确
            if (this.code != this.userCode) {
                Toast('验证码不正确');
                return;
            }

            //如果正确，发送请求，后端进行注册
            const addUserModel = new AddUserModel();
            addUserModel.addUser(this.userTel,this.userPwd).then(res => {
                const data = res.data;

                //提示信息
                Toast('注册成功，请重新登录');
                //登录失败
                if (!data.success) return;
                //登录成功 ==》跳转页面，存储用户信息
                // this.USER_LOGIN(res.data);
                //跳转到登录页面
                this.$router.push('/pwdLogin');
            })
        }
    }
}
</script>

<style scoped lang='scss'>
@import '~styles/mixins.scss';
@import '~styles/variables.scss';

.registration-wrapper {
    @include flex-column;
    margin-top: 2.8rem;
    width: 100%;
    font-size: .16rem;
    box-sizing: border-box;
    
    .login-tel {
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
	    box-sizing: border-box;

        input {
            width: 100%;
			height: 100%;
			border: 1px solid rgb(244, 165, 134);
			border-radius: .03rem;
			box-sizing: border-box;
			text-indent: .1rem;
        }
    }

    .login-code {
        display: flex;
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
	    box-sizing: border-box;

        input {
            flex: 0 0 2rem;
            height: 100%;
            // width: 2rem;
            border: 1px solid rgb(244, 165, 134);
            border-radius: .03rem;
            box-sizing: border-box;
            text-indent: .1rem;
        }

        button{
            flex: 1;
            height: 100%;
            padding:0 0.1rem;
            color:$defaultColor;
            background-color:rgb(244, 165, 134);
            border-radius: .03rem;
            box-sizing: border-box;
        }
    }

    .login-btn{
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
	    box-sizing: border-box;
	    

        button {
            width: 100%;
			height: 100%;
            text-align: center;
            color:$defaultColor;
            background-color: rgb(244, 165, 134);
            border: 1px solid rgb(244, 165, 134);
			border-radius: .03rem;
			box-sizing: border-box;
        }
	}
	.tab{
		display: flex;
        justify-content: space-between;
        font-size: .14rem;
        padding: .05rem .15rem;
	    box-sizing: border-box;
        color: rgb(244, 165, 134);
	}
}
</style>

